package com.dfbz.dao;

import com.dfbz.entity.User;
import com.dfbz.utils.PageUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class UserDao extends BaseDao{

    public User findByUsername(String username) {
        try {
            return jdbcTemplate.queryForObject("select * from user where username = ?",new BeanPropertyRowMapper<>(User.class),username);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }

    }

    public User findByemail(String email) {
        try {
            return jdbcTemplate.queryForObject("select * from user where email = ?",new BeanPropertyRowMapper<>(User.class),email);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }

    }

    public void addUser(User user) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        jdbcTemplate.update("insert into user values (null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                user.getUsername(),
                user.getPassword(),
                user.getEmail(),
                user.getQqOpenid(),
                user.getWxOpenid(),
                user.getRealName(),
                user.getAge(),
                user.getPhone(),
                user.getGender(),
                user.getInfo(),
                format.format(user.getRegisterTime()),
                user.getLoginTime(),
                user.getPic(),
                user.getLook(),
                user.getIsSecret(),
                user.getDeptName(),
                user.getDeptId()
        );
    }

    public PageUtils<User> findUserList(int cureePage, String realName) {
        PageUtils<User> userPageUtils = new PageUtils<>();
        Integer totalCount = jdbcTemplate.queryForObject("select count(1) from user where real_name like ?",Integer.class,"%"+realName+"%");
        Integer pageSize = userPageUtils.getPageSize();
        Integer pageCount = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize)+1;
        List<User> userList = jdbcTemplate.query("select * from user where real_name like ? limit ?,?", new BeanPropertyRowMapper<>(User.class) ,"%"+realName+"%",(cureePage-1) * pageSize,pageSize);
        userPageUtils.setPageCount(pageCount);
        userPageUtils.setObjList(userList);
        userPageUtils.setTotalCount(totalCount);
        return userPageUtils;
    }

    public List<User> findUserByDeptId(Long id) {
        try {
            return jdbcTemplate.query("select real_name,id from user where dept_id = ?",new BeanPropertyRowMapper<>(User.class),id);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public User findUserById(long id) {
        User user = jdbcTemplate.queryForObject("select * from user where id = ?",new BeanPropertyRowMapper<>(User.class), id);

        return user;

    }

    public Long findFocus(long id) {
        try {
            return jdbcTemplate.queryForObject("select count(1) from userfocus where user_id = ?",Long.class,id);
        } catch (DataAccessException e) {
            return 0L;
        }

    }

    public Long findFans(long id) {
        try {
            return jdbcTemplate.queryForObject("select count(1) from userfocus where user_focus_id = ?",Long.class,id);
        } catch (DataAccessException e) {
            return 0L;
        }
    }

    public void updateLookById(Long id) {
        jdbcTemplate.update("update user set look = look +1 where id = ?",id);
    }

    public void updatePic(Long id, String path) {
        jdbcTemplate.update("update user set pic = ? where id = ?",path,id);
    }

    public void updateUserInfo(User user) {
        jdbcTemplate.update(
                "update user set password=?,real_name=?,age=?,phone=?,gender=?,`info`=?,is_secret=?,dept_name=?,dept_id=? where id=?",
                user.getPassword(),
                user.getRealName(),
                user.getAge(),
                user.getPhone(),
                user.getGender(),
                user.getInfo(),
                user.getIsSecret(),
                user.getDeptName(),
                user.getDeptId(),
                user.getId());
    }


    public  PageUtils<User> findUserListById(Integer cureePage,List<Long> focusList) {
        PageUtils<User> userPageUtils = new PageUtils<>();
        StringBuilder sql = new StringBuilder("select id,real_name from user where id in(");
        for (int i = 0; i < focusList.size(); i++) {
            sql.append(focusList.get(i));
            if(i == focusList.size()-1){
                sql.append(")");
            }else{
                sql.append(",");
            }
        }
        Integer pageSize = userPageUtils.getPageSize();
        Integer totalCount = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(User.class)).size();
        List<User> userList = jdbcTemplate.query(sql.append(" limit ?,?").toString(), new BeanPropertyRowMapper<>(User.class),(cureePage-1) * pageSize,pageSize);
        Integer pageCount = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize)+1;
        userPageUtils.setPageCount(pageCount);
        userPageUtils.setObjList(userList);
        userPageUtils.setTotalCount(totalCount);
        userPageUtils.setCureePage(cureePage);
        return userPageUtils;
    }

    public void updateLoginTimeById(Long id) {
        jdbcTemplate.update("update user set login_time = ? where id=?",new Date(),id);
    }

    public User findUserByWxopenid(String wxOpenid) {
        try {
            return jdbcTemplate.queryForObject("select * from user where wx_openid = ?",new BeanPropertyRowMapper<>(User.class),wxOpenid);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public Long findMaxId() {
       return jdbcTemplate.queryForObject("select max(id) from user",Long.class);
    }
}
